package com.example;

import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.example.aggregator.Aggregator;
import com.example.collector.MetricsCollector;
import com.example.collector.RequestInfo;
import com.example.reporter.ConsoleReporter;
import com.example.storage.MetricsStorage;
import com.example.storage.RediesMetricsStorate;
import com.example.viewer.ConsoleViewer;
import com.example.viewer.Viewer;

/**
 * Hello world!
 *
 */
public class App {

    private static ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

    public static void main(String[] args) {
        System.out.println("Hello World!");

        // InetAddress.getLocalHost().getHostName();

        // 存储
        final MetricsStorage storate = new RediesMetricsStorate();

        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                // 数据收集
                MetricsCollector collector = new MetricsCollector(storate);
                Random random = new Random();
                int randomInt = random.nextInt(100);
                long curTimeInMillis = System.currentTimeMillis();
                if (randomInt > 30) {
                    if (randomInt % 2 == 0) {
                        collector.recordRequest(new RequestInfo("test", curTimeInMillis, curTimeInMillis + randomInt));
                    } else {
                        collector.recordRequest(new RequestInfo("user", curTimeInMillis, curTimeInMillis + randomInt));
                    }
                }
            }
        }, 0, 10, TimeUnit.MILLISECONDS);

        // 数据
        Aggregator aggregator = new Aggregator();
        Viewer viewer = new ConsoleViewer();
        ConsoleReporter reporter = new ConsoleReporter(storate, aggregator, viewer);
        reporter.startReporter(5, 5);

    }
}
